08. Write Your First Test
L5 A06 Write Your First Test (No TDD) Part 1 V2
Now it is is your turn to write your first tests.
Step 1: Create a Test Class
- Right click
getActiveAndCompletedStatsand select Generate then Test.
Then Create Test dialog opens.
- Change the Class name to
StatisticsUtilsTest. - Keep the rest of the defaults and press OK.
Then Choose Destination Directory dialog opens.
- Select the test directory (not
androidTest) because you'll be writing local tests. Press OK.
Step 2: Write Your First Test
StatisticsUtilsTest.kt
class StatisticsUtilsTest {
@Test
fun getActiveAndCompletedStats_noCompleted_returnsHundredZero() {
// Create an active tasks (the false makes this active)
val tasks = listOf<Task>(
Task("title", "desc", isCompleted = false)
)
// Call our function
val result = getActiveAndCompletedStats(tasks)
// Check the result
assertEquals(result.completedTasksPercent, 0f)
assertEquals(result.activeTasksPercent, 100f)
}
}
- Go ahead and run the test (Right click
StatisticsUtilTestand select Run).
Step 3: Write More Tests Yourself
Using the test above and the video, write the following tests:
- Start by writing test for when you have a normal task list:
- If there is one completed task and no active tasks, the
activeTaskspercentage should be 0f, the completed tasks percentage should be 100f. - If there are two completed tests and three active test, the completed percentage should be 40f and the active percentage should be 60f.
- If there is one completed task and no active tasks, the